﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using pTools.cMethods;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;

namespace pTools.TuQiangPrint
{
    public partial class SelectInventory : DevExpress.XtraEditors.XtraForm
    {
        DBHelper db = new DBHelper();

        public SelectInventory()
        {
            InitializeComponent();
        }

        private void NewSelectInventory_Load(object sender, EventArgs e)
        {
            LoadInvtory();
        }

        void LoadInvtory()
        {
            var sql = @"select proCode ,proName ,spec  from fun_Inventory_Info
where isDel=0 and status='Y' and baseCode = (select DbCode from sys_CustomerBase_info) ";

            var proCode = tb_proCode.Text;
            var proName = tb_proName.Text;

            if (!string.IsNullOrEmpty(proCode))
            {
                sql += " and procode like '%" + proCode + "%' ";
            }
            if (!string.IsNullOrEmpty(proName))
            {
                sql += " and proName like '%" + proName + "%' ";
            }
            
            var dt = db.getData(sql);
            grid_Invtory.DataSource = dt;
        }

        private void tb_EditValueChanged(object sender, EventArgs e)
        {
            LoadInvtory();
        }

        private void gridView_Invtory_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            Point pt = grid_Invtory.PointToClient(Control.MousePosition);
            GridHitInfo info = gridView_Invtory.CalcHitInfo(pt);

            if (gridView_Invtory.IsRowSelected(e.RowHandle) && info.Column.VisibleIndex != 0)
            {
                gridView_Invtory.UnselectRow(e.RowHandle);
            }
            else if (!gridView_Invtory.IsRowSelected(e.RowHandle) && info.Column.VisibleIndex != 0)
            {
                gridView_Invtory.SelectRow(e.RowHandle);
            }
        }

        private void btn_Confirm_Click(object sender, EventArgs e)
        {
            var dic = new Dictionary<string, string>();

            int[] djRows = this.gridView_Invtory.GetSelectedRows(); //获取选中行号；
            DataRow djRow ;

            if (djRows.Length > 0)
            {
                if (djRows.Length > 1)
                {
                    XtraMessageBox.Show("请选择一条存货信息");
                    return;
                }

                djRow = this.gridView_Invtory.GetDataRow(djRows[0]);    //根据行号获取相应行的数据；

                dic["proCode"] = djRow["proCode"].ToString();
                dic["proName"] = djRow["proName"].ToString();
                dic["spec"] = djRow["spec"].ToString();
                CommonMethods.Parameters = dic;
            }
            else
            {
                XtraMessageBox.Show("未选择存货信息");
                return;
            }

            this.Close();
        }
    }
}
